package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.metric.MetricServiceEntity;
import com.hllxd.hism.star.basic.service.service.DataBaseService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 数据服务
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/data_content")
public class DataBaseServiceController2 {

    @Autowired
    private DataBaseService dataBaseService;

    @Data
    public static class RequestDefine {
        public Long id;
    }
    /**
     * request param.
     */
    @Data
    public static class Param {
        /**服务信息 ***/
       private  long  id;
    }



    /*****************  *******服务能力部分 start********/
    /**
     * 基础服务信息列表
     * @param params
     * @param result
     * @return
     */
    @RestGrpcMapping("/listMetricServiceEntity.do")
    public NTSTATUS listMetricServiceEntity(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        try {
            List<MetricServiceEntity> items = dataBaseService.listMetricServiceEntity();
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 单一服务详情信息
     * @param params
     * @param result
     * @return
     */
    @RestGrpcMapping("/detailMetricServiceEntity.do")
    public NTSTATUS    detailMetricServiceEntity(PDRIVER_OBJECT params, PRESULT_OBJECT result /*, @PathParam("id") long id*/) {
        try {
            System.out.println("---->detailMetricServiceEntity");
            DataBaseServiceController2.Param aa = params.driverObject(DataBaseServiceController2.Param.class);

            Long id = aa.id;

            List<MetricServiceEntity> items = dataBaseService.listMetricServiceDetail(id);
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/listMetricSource.do")
    public NTSTATUS listMetricSource(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params====>: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/modifySearch.do")
    public NTSTATUS modify(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params====>: {}", params);
        try {

            AdtypeDefineController2.RequestDefine aa = params.driverObject(AdtypeDefineController2.RequestDefine.class);
            Long id = aa.getId();
            List<MetricServiceEntity> items = dataBaseService.listMetricServiceDetail(id);
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/modifyAdd.do")
    public NTSTATUS add(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            MetricServiceEntity aa = params.driverObject(MetricServiceEntity.class);
            Object res = dataBaseService.modifyServiceEntity(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 删除单一服务元素。
     * @param params
     * @param result
     * @return
     */
    @RestGrpcMapping("/delMetricSource.do")
    public NTSTATUS delMetricSource(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params====>: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            Long id = aa.getId();
            long items = dataBaseService.deleteService(id);
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 添加服务元素。
     * @param params
     * @param result
     * @return
     */
    @RestGrpcMapping("/addServiceItem.do")
    public NTSTATUS addServiceItem(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        System.out.println("will addServiceItem start ........");
        log.info("request params: {}", params);
        try {
            MetricServiceEntity aa = params.driverObject(MetricServiceEntity.class);
            Long res = dataBaseService.addServiceEntity(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }





        /************************服务能力部分 end********/





}
